package com.yy.bootdemo.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.bootdemo.admin.dto.MenuIndexDto;
import com.yy.bootdemo.admin.entity.MyImgEntity;
import com.yy.bootdemo.admin.entity.MyUser;
import com.yy.bootdemo.admin.service.UserService;
import com.yy.bootdemo.common.utils.PageTableRequest;
import com.yy.bootdemo.common.utils.Result;
import com.yy.bootdemo.common.utils.UserConstants;
import com.yy.bootdemo.log.aop.MyLog;
import com.yy.bootdemo.oss.UploadFile;
import com.yy.bootdemo.security.dto.JwtUserDto;
import com.yy.bootdemo.security.utils.PageUtils;
import com.yy.bootdemo.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author yy
 * @createTime 2022/8/1
 */
@RestController
@RequestMapping("user")
@Api(tags = "系统：用户管理")
public class UserController {
    @Autowired
    private UserService userService;



    @GetMapping("/getUserByid")
    public  MyUser getUserByid(@RequestParam Integer userId) {
        return userService.getUserById(userId);
    }



    @PostMapping(value = "/list")
    @PreAuthorize("hasAnyAuthority('user:list')")
    @ApiOperation(value = "用户列表")
    public List<MenuIndexDto> list() {

        return userService.getUserlist();
    }

    @MyLog("查询用户")
    @RequestMapping("/pagelist")
    public R pagelist(@RequestBody PageUtils params){
        // 条件构造器
        QueryWrapper<MyUser> userQuery = Wrappers.query();


        Page<MyUser> page = new Page<>(params.getCurrent(), params.getSize());

        IPage<MyUser> userPage = userService.page(page, userQuery);

        return R.ok(userPage);
    }
    @GetMapping("getUserList")
    public R getUserList(){
        // 条件构造器
        QueryWrapper<MyUser> userQuery = Wrappers.query();

        userQuery.in("type",0,1);
        List<MyUser> list=  userService.list(userQuery);
        return R.ok(list);
    }


    @GetMapping("getById")
    public R getUser(@RequestParam Integer userId){
        return R.ok(userService.getById(userId));
    }



    @MyLog("添加用户")

    @PostMapping("save")
    @PreAuthorize("hasAnyAuthority('user:add')")
    public R save1(@RequestBody MyUser myMenu){
        userService.saveUser(myMenu);
        return R.ok();
    }

    @PostMapping("updateById")
    @PreAuthorize("hasAnyAuthority('user:edit')")
    public R updateById(@RequestBody MyUser myMenu){
        userService.updateUser(myMenu);
        return R.ok();
    }


    @RequestMapping("/uploadImg")
    public R uploadImg( @RequestParam(value = "imgFile", required = false) MultipartFile file) {

        JwtUserDto jwtUserDto = (JwtUserDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userId = jwtUserDto.getMyUser().getUserId();
        //获取上传文件输入流
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //获取文件名称
        String fileName = file.getOriginalFilename();
        String url=  UploadFile.uploadFileAvatar(inputStream,fileName);
        MyUser myUser=  new MyUser();
        myUser.setUserId(userId);
        myUser.setImg(url);
        userService.updateById(myUser);
        return R.ok();
    }











    @PostMapping("save1")
    @PreAuthorize("hasAnyAuthority('user:add')")
    @MyLog("添加用户")
    public R saveUser(@RequestBody MyUser myUser){
        if (UserConstants.USER_PHONE_NOT_UNIQUE.equals(userService.checkPhoneUnique(myUser))){
            return R.error("手机号已存在");
        }
        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(userService.checkUserNameUnique(myUser))){
            return R.error("用户名已存在");
        }
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        myUser.setPassword(bCryptPasswordEncoder.encode("123456"));
        return userService.save(myUser,myUser.getRoleId());
    }


}
